package com.hilon.base.controller;

import com.hilon.base.service.EntityHandleService;
import com.hilon.segment.mybatis.utils.ResultBean;
import com.sun.istack.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

public class BaseController<T> {

    private EntityHandleService entityHandleService;

    protected void buildEntityHandleService(EntityHandleService entityHandleService){
        this.entityHandleService=entityHandleService;
    }

    @PostMapping("/find-all")
    public ResultBean<List<T>> findAll() {
        return entityHandleService.findAll();
    }


    @PostMapping("/search")
    public ResultBean<List<T>> listEntityByCondition(@RequestBody T entity) {
        return entityHandleService.listEntityByCondition(entity);
    }

    @PostMapping("/save")
    public ResultBean<Void> saveEntity(@Validated @RequestBody T entity) {

        return entityHandleService.saveEntity(entity);
    }

    @PostMapping("/save-all")
    public ResultBean<Void> saveAll(@Validated @RequestBody List<T> entities) {
        return entityHandleService.saveAll(entities);
    }

    @PutMapping("/update/{id}")
    public ResultBean<Void> updateDepartment(@NotNull @PathVariable Long id, @Validated @RequestBody T entity) {
        return entityHandleService.updateEntity(id, entity);
    }

    @PostMapping("/batch-delete")
    public ResultBean<Void> deleteBatchEntityById(@NotNull @RequestBody List<Long> ids) {
        return entityHandleService.deleteBatchEntityById(ids);
    }
}
